package comunicacion;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Conversion_archivos {
 
    
    public  byte[] a_bytes(File file)
    {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buf = new byte[1024];
            int i=0;
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                i++;
                bos.write(buf, 0, readNum); //no doubt here is 0
                //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                //System.out.println("read " + readNum + " bytes,*"+i);
            
        
    }   } catch (IOException ex) {
            Logger.getLogger(Conversion_archivos.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(Conversion_archivos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
         
     return bos.toByteArray();
        
        
    }
    public  void convertir_bytes(byte [] bytes,String archivo){
        //System.err.print("_________________________________entro_____________________________");
        FileOutputStream fos = null;
        try {
            
            File someFile = new File(archivo);
            fos = new FileOutputStream(someFile);
            fos.write(bytes);
            fos.flush();
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(Conversion_archivos.class.getName()).log(Level.SEVERE, null, ex);
        }  finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Conversion_archivos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.err.println("el archivo a sido guardado");
        
    }
    
}